﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

namespace NMA.Web.Core.Helper
{
    using NMA.Web.Areas.Admin.Models;

    /// <summary>
    /// "If there's an IF, make a Helper"
    /// http://blog.wekeroad.com/blog/asp-net-mvc-avoiding-tag-soup/
    /// </summary>
    public static class PagingHelper
    {
        public static string RenderingPager(this HtmlHelper helper, PagingModel model)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendLine("<div class=\"separate\">&nbsp;</div>");
            builder.AppendFormat("<div class=\"totalitem clear\">{0}-{1} of {2} records</div>", model.MinItemIndexOfCurrentPage, model.MaxItemIndexOfCurrentPage, model.TotalCount);
            builder.AppendFormat("<div class=\"movementbar\" style=\"display:{0}\">", model.TotalPages > 1 ? "block" : "none");
            builder.AppendFormat("<ul class=\"pagination {0}\">", model.CssClass);

            if (model.HasPreviousPage)
            {
                builder.AppendFormat("<li class=\"previous\"><a href=\"{0}\" class=\"page{1}\">Previous</a></li>", model.PageActionLink.Replace("{page}", (model.PageIndex - 1).ToString()), model.PageIndex - 1);
            }
            else
            {
                builder.AppendLine("<li class=\"previous\">Previous</li>");
            }

            for (int page = model.MinPageIndexForDisplaying; page <= model.MaxPageIndexToDisplay; page++)
            {
                if (page == model.PageIndex)
                {
                    builder.AppendFormat("<li class=\"active\">{0}</li>", page);
                }
                else
                {
                    builder.AppendFormat("<li><a href=\"{0}\" class=\"page{1}\">{2}</a></li>", model.PageActionLink.Replace("{page}", page.ToString()), page, page);
                }
            }

            if (model.HasNextPage)
            {
                builder.AppendFormat("<li class=\"next\"><a href=\"{0}\" class=\"page{1}\">Next</a></li>", model.PageActionLink.Replace("{page}", (model.PageIndex + 1).ToString()), model.PageIndex + 1);
            }
            else
            {
                builder.AppendLine("<li class=\"next\">Next</li>");
            }

            return builder.ToString();
        }
    }
}